import { Component, Inject, ViewChild} from '@angular/core';
import { IonicPage ,ModalController ,Tabs } from 'ionic-angular';
// import * as Constant from '../../constant/constant';
import * as PageConstant from '../../constant/pageConstant';
import {Observable} from "rxjs/Observable";

/**
 * tab页
 */
@IonicPage()
@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tab1Root: string= PageConstant.BorrowMoneyPage; // 借钱
  tab2Root: string= PageConstant.GiveBackMoneyPage; // 还款
  tab3Root: string= PageConstant.MinePage; // 我的

  @ViewChild('myTabs') tabRef: Tabs;
  private authState$: Observable<any>;

  protected selectedIndex: number= 0; // 当前选择的index

  constructor(
    private modalCtrl: ModalController,
    @Inject('auth') private authService,
  )
  {
    this.authState$= this.authService.getAuth()
  }

  /*** 检查tabs覆盖状态处理
  * */
  public clickHandleTabs(index: number): void
  {
    //检查登录状态
    this.authService.getAuth()
      .first()
      .subscribe(auth=> {
      // if(auth.errMsg=== Constant.LOGIN_AGAIN) {
      //   this.navCtrl.setRoot(PageConstant.LoginPage);
      //   return;
      // }

      if(auth.hasError) {
        const modal = this.modalCtrl.create(PageConstant.LoginPage, {},{
          showBackdrop: false,
          enableBackdropDismiss: false,
        });
        // modal.onDidDismiss(data => {
        //   debugger;
        // });
        modal.present();
        return;
      }
      this.tabRef.select(index);
    });
  }
}
